package com.liuxu.myxposed;

import android.app.ActivityManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.liuxu.myxposed.Utis.Mnt;
import com.liuxu.myxposed.Utis.SharedPref;
import com.liuxu.myxposed.Utis.ShellUtils;

import java.io.File;
import java.io.RandomAccessFile;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Button button;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                while (true) {
                    String appname = "com.tencent.reading";
                    if (isServiceStarted(getApplicationContext(), appname)) {
                        Toast.makeText(MainActivity.this, toastMessage(), Toast.LENGTH_SHORT).show();
                        ShellUtils.execCommand(appname, true);
                    }
                }
            }
        });
    }

    public String toastMessage() {
        String phoneInfo="开始运行";
        return String.valueOf(phoneInfo);
    }
    public static  boolean isServiceStarted(Context context, String PackageName)
    {
        boolean isStarted =false;
        try
        {
            ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
            int intGetTastCounter = 1000;
            List<ActivityManager.RunningServiceInfo> mRunningService =
                    mActivityManager.getRunningServices(intGetTastCounter );
            for (ActivityManager.RunningServiceInfo amService : mRunningService)
            {
                if(0 == amService.service.getPackageName().compareTo(PackageName))
                {
                    isStarted = true;
                    break;
                }
            }
        }
        catch(SecurityException e)
        {
            e.printStackTrace();
        }
        return isStarted;
    }



}
